package com.book.iphone;

import com.book.book.domain.BooksDO;
import com.book.book.domain.CategoryDO;
import com.book.book.service.BooksService;
import com.book.book.service.CategoryService;
import com.book.iphone.domain.CateBook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：青松
 * 日期： 2018/3/13
 */
@Controller
@RequestMapping("/iphone")
public class BooksClassifyController {
    @Autowired
    CategoryService categoryService;
    @Autowired
    BooksService booksService;
    @RequestMapping("/classify/{Key}")
    String bookListByClassify(@PathVariable String Key, Model model){
        model.addAttribute("title",Key);
        List<CategoryDO> categoryDOList=categoryService.list(new HashMap<>());
        Integer id=0;
        for (CategoryDO categoryDO:categoryDOList){
            if (categoryDO.getCatName().equals(Key)){
                id=categoryDO.getCatId();
                break;
            }
        }//拿到图书分类id，根据分类id查找图书list
        Map<String,Object> map=new HashMap<>();
        map.put("catId",id);
        List<BooksDO> booksDOList =booksService.list(map);
        if (booksDOList!=null||booksDOList.size()!=0){
            for (BooksDO booksDO:booksDOList){
                String imgurl=booksDO.getBookImg();
                if (imgurl!=null||imgurl!=""){
                    booksDO.setBookImg(imgurl.split(",")[0]);
                }
            }//遍历list，分离url
            model.addAttribute("booksDOList",booksDOList);
        }else {
            model.addAttribute("booksDOList",null);
        }
        return "/book/phone/html/ClassifyBooks";
    }
}
